class Watcher {
  clientList = {}
  constructor() {
    this.clientList = {}
  }
  $emit(key, ...args) {
    const fns = this.clientList[key]
    if (!fns || !fns.length) {
      return false
    }
    for (let i = 0, fn; (fn = fns[i++]); ) {
      fn.apply(this, args)
    }
  }
  $on(key, fn) {
    if (!this.clientList[key]) {
      this.clientList[key] = []
    }
    this.clientList[key].push(fn)
  }
  $off(key, fn) {
    const fns = this.clientList[key]
    if (!fns || !fns.length) {
      return false
    }
    if (!fn) {
      fns && (fns.length = 0)
    } else {
      for (let l = fns.length - 1; l >= 0; l--) {
        const _fn = fns[l]
        if (_fn === fn) {
          fns.splice(l, 1)
        }
      }
    }
  }
}

const instance = new Watcher()

export default instance

